home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Panorama / Panorama - Disk 01 (1986-02-15)(Pacific North-West Amigas Club)[h AFL][b corrupt files].zip / Panorama - Disk 01 (1986-02-15)(Pacific North-West Amigas Club)[h AFL][b corrupt files].adf / mand5.c < prev    next >
Text File  |  1989-10-24  |  13KB  |  439 lines

  1. /*****************************************************************************
  2.  *                     MAND5.C - Color Window
  3.  *             Mandelbrot Self-Squared Dragon Generator
  4.  *                    For the Commodore Amiga
  5.  *                         Version 2.01
  6.  *
  7.  *             Copyright (C) 1986, =Robert J. Mical=
  8.  *                  Placed in the Public Domain
  9.  *
  10.  *  This program may be distributed free of charge as long as the above
  11.  *  notice is retained.  You may extract any or all information contained
  12.  *  in this file for use in your own programs
  13.  *
  14.  ****************************************************************************/
  15.  
  16. #include "mand.h"
  17.  
  18.  
  19. extern struct Menu MainMenu[MENU_COUNT];
  20. extern struct MenuItem OptionsItems[OPTIONS_COUNT];
  21.  
  22. extern int MathBase, MathTransBase;
  23.  
  24. extern SHORT Color0, Color1, Color2;
  25. extern SHORT UserPalette[29];
  26.  
  27. /*----------------------*/
  28. /* Graphics definitions */
  29.  
  30. extern struct   GfxBase       *GfxBase;
  31. extern struct   IntuitionBase *IntuitionBase;
  32.  
  33. extern struct   RastPort      *rp,*rp2;
  34. extern struct   ViewPort      *vp;
  35.  
  36. extern struct   Window        *w,*w2, *ColorWindow;
  37. extern struct   Screen        *screen;
  38. extern struct   IntuiMessage  *message;
  39.  
  40. extern long last_color;
  41.  
  42. extern BOOL SettingCenter, SettingBoxSize;
  43.  
  44. /*----------------------------------*/
  45. /* Miscellaneous Global Definitions */
  46.  
  47. extern union kludge {
  48.    float f;
  49.    int i;
  50. } start_r,end_r,start_i,end_i;  /* Block bounds for set */
  51. extern int max_x,max_y,max_mem_y;  /* Graphics window size */
  52. extern int max_count,color_inc,color_offset,color_set,color_mode,color_div;
  53. extern int color_inset,func_num;
  54.  
  55. extern int v_starty,max_mem;
  56. extern long v_offset;
  57. extern UWORD *color_table,*v_mand_store;
  58.  
  59. extern int modified,want_read;
  60.  
  61. extern FILE *console,*v_fp,*redir_fp;
  62.  
  63. extern SHORT ZoomCenterX, ZoomCenterY, ZoomBoxSizeX, ZoomBoxSizeY;
  64. extern SHORT ZoomBoxStartX, ZoomBoxStartY;
  65.  
  66.  
  67.  
  68. /*****************************************************************************
  69.  *
  70.  * Color initialization and routines
  71.  *
  72.  ****************************************************************************/
  73.  
  74. #define COPYCOLOR    1
  75. #define RANGE_FIRST  2
  76. #define RANGE_SECOND 3
  77. USHORT ColorMode;
  78.  
  79. extern struct Gadget ColorTemplateGadgets[COLOR_GADGETS_COUNT];
  80. extern struct Image ColorPropsImages[3];
  81. extern struct PropInfo ColorPropsInfos[3];
  82. extern struct IntuiText ColorClusterText[4];
  83.  
  84. USHORT SavePalette[32];
  85. extern struct Image SuperColorImages[32];
  86.  
  87. #define COLOR_IDCMP_FLAGS (GADGETDOWN | GADGETUP | MOUSEBUTTONS \
  88.         | MENUPICK | MOUSEMOVE | ACTIVEWINDOW | INACTIVEWINDOW)
  89.  
  90.  
  91. struct NewWindow ColorNewWindow =
  92.     {
  93.     /*  SHORT LeftEdge, TopEdge;        /* screen dimensions of window */
  94.     /*  SHORT Width, Height;            /* screen dimensions of window */
  95.     DEFAULT_WINDOW_LEFT, DEFAULT_WINDOW_TOP,
  96.     COLORWINDOW_WIDTH, COLORWINDOW_HEIGHT,
  97.  
  98.     /*  UBYTE DetailPen, BlockPen;      /* for bar/border/gadget rendering */
  99.     -1, -1,
  100.  
  101.     /*  ULONG IDCMPFlags;               /* User-selected IDCMP flags */
  102.     NULL,
  103.  
  104.     /*  ULONG Flags;                    /* see Window struct for defines */
  105.     BORDERLESS | SMART_REFRESH | NOCAREREFRESH,
  106.  
  107.     /*  struct Gadget *FirstGadget;*/
  108.     NULL,
  109.  
  110.     /*  struct Image *CheckMark;*/
  111.     NULL,
  112.  
  113.     /*  UBYTE *Title;                     /* the title text for this window */
  114.     NULL,
  115.     
  116.     /*  struct Screen *Screen;*/
  117.     NULL,
  118.     
  119.     /*  struct BitMap *BitMap;*/
  120.     NULL,
  121.  
  122.     /*  SHORT MinWidth, MinHeight;       /* minimums */
  123.     0, 0,
  124.     /*  SHORT MaxWidth, MaxHeight;       /* maximums */
  125.     0, 0,
  126.  
  127.     /*  USHORT Type;*/
  128.     CUSTOMSCREEN,
  129. };
  130.  
  131. USHORT RangeFirst;      /* the first selection of the range-color pair */
  132.  
  133.  
  134. /* ======================================================================== */
  135. /* ======================================================================== */
  136. /* ======================================================================== */
  137.  
  138. BOOL OpenColorWindow()
  139. {
  140.     SHORT i;
  141.  
  142.     if (ColorWindow) return(TRUE);
  143.  
  144.     ClearMenuStrip(w);
  145.     ColorNewWindow.Screen = screen;
  146.     ColorNewWindow.IDCMPFlags = NULL;
  147.     ColorNewWindow.FirstGadget
  148.             = &ColorTemplateGadgets[COLOR_GADGETS_COUNT - 1];
  149.     SetColorProps();
  150.     InitSuperColors();
  151.     ColorMode = NULL;
  152.  
  153.     if ((ColorWindow = (struct Window *)OpenWindow(&ColorNewWindow)) == 0)
  154.         {
  155.         fputs("no memory!\n", console);
  156.         return(FALSE);
  157.         }
  158.     ColorWindow->UserPort = w->UserPort;
  159.     ModifyIDCMP(ColorWindow, COLOR_IDCMP_FLAGS);
  160.     SetMenuStrip(w, &MainMenu[0]);
  161.  
  162.     for (i = 0; i < 32; i++) SavePalette[i] = GetRGB4(vp->ColorMap, i);
  163.     DrawColorWindow();
  164.     return(TRUE);
  165. }
  166.  
  167.  
  168. VOID CloseColorWindow(accept)
  169. BOOL accept;
  170. {
  171.     SHORT i;
  172.  
  173.     if (ColorWindow == NULL) return;
  174.  
  175.     ClearMenuStrip(ColorWindow);
  176.     ColorWindow->UserPort = NULL;
  177.     CloseWindow(ColorWindow);
  178.     ColorWindow = NULL;
  179.  
  180.     if (NOT accept) LoadRGB4(vp, &SavePalette[0], 32);
  181.     else
  182.         {
  183.         color_set = 2;
  184.         Color0 = GetRGB4(vp->ColorMap, 0);
  185.         Color1 = GetRGB4(vp->ColorMap, 1);
  186.         Color2 = GetRGB4(vp->ColorMap, 2);
  187.         for (i = 2; i <= 31; i++)
  188.             {
  189.             if (i == 2) *(color_table) = GetRGB4(vp->ColorMap, 2);
  190.             else *(color_table + color_offset + (i - 2) * color_inc) 
  191.                     = GetRGB4(vp->ColorMap, i);
  192.             }
  193.         }
  194. }
  195.  
  196.  
  197. VOID ColorRange(first, last)
  198. SHORT first, last;
  199. {
  200.     SHORT i;
  201.     LONG whole, redfraction, greenfraction, bluefraction;
  202.     USHORT rgb;
  203.     SHORT firstred, firstgreen, firstblue;
  204.     SHORT lastred, lastgreen, lastblue;
  205.     SHORT workred, workgreen, workblue;
  206.  
  207.     if (first > last)
  208.         {
  209.         i = first;
  210.         first = last;
  211.         last = i;
  212.         }
  213.  
  214.     /* I need to see a spread of at least two, where there's at least one
  215.      * spot between the endpoints, else there's no work to do so I
  216.      * might as well just return now.
  217.      */
  218.     if (first >= last - 1) return;
  219.  
  220.     rgb = GetRGB4(vp->ColorMap, first);
  221.     firstred = (rgb >> 8) & 0xF;
  222.     firstgreen = (rgb >> 4) & 0xF;
  223.     firstblue = (rgb >> 0) & 0xF;
  224.  
  225.     rgb = GetRGB4(vp->ColorMap, last);
  226.     lastred = (rgb >> 8) & 0xF;
  227.     lastgreen = (rgb >> 4) & 0xF;
  228.     lastblue = (rgb >> 0) & 0xF;
  229.  
  230.     whole = (lastred - firstred) << 16;
  231.     redfraction = whole / (last - first);
  232.     whole = (lastgreen - firstgreen) << 16;
  233.     greenfraction = whole / (last - first);
  234.     whole = (lastblue - firstblue) << 16;
  235.     bluefraction = whole / (last - first);
  236.  
  237.     for (i = first + 1; i < last; i++)
  238.         {
  239.         lastred = (redfraction * (i - first) + 0x8000) >> 16;
  240.         workred = firstred + lastred;
  241.         lastgreen = (greenfraction * (i - first) + 0x8000) >> 16;
  242.         workgreen = firstgreen + lastgreen;
  243.         lastblue = (bluefraction * (i - first) + 0x8000) >> 16;
  244.         workblue = firstblue + lastblue;
  245.         SetRGB4(vp, i, workred, workgreen, workblue);
  246.         }
  247. }
  248.  
  249.  
  250. BOOL ColorGadgetGotten(gadget)
  251. struct Gadget *gadget;
  252. {
  253.     USHORT rgb, pen;
  254.     struct RastPort *RPort;
  255.     SHORT greenpos, redpos, bluepos;
  256.  
  257.     RPort = ColorWindow->RPort;
  258.  
  259.     /* Have we got a color specifier? */
  260.     if (gadget->GadgetID < 32)
  261.         {
  262.         /* Yes, it's a color gadget.  Set this pen number */
  263.         pen = gadget->GadgetID;
  264.         /* first, were we in COPY COLOR mode? */
  265.         if (ColorMode == COPYCOLOR)
  266.             {
  267.             /* ok, copy old color here first! */
  268.             rgb = GetRGB4(vp->ColorMap, rp->FgPen);
  269.             SetRGB4(vp, pen, rgb >> 8, rgb >> 4, rgb);
  270.             ColorMode = NULL;
  271.             }
  272.         if (ColorMode == RANGE_FIRST)
  273.             {
  274.             ColorMode = RANGE_SECOND;
  275.             RangeFirst = pen;
  276.             }
  277.         else if (ColorMode == RANGE_SECOND)
  278.             {
  279.             ColorMode = NULL;
  280.             ColorRange(RangeFirst, pen);
  281.             }
  282.         SetAPen(rp, pen);
  283.         rgb = GetRGB4(vp->ColorMap, pen);
  284.         ColorRectFill(RPort, pen);
  285.  
  286.         redpos = RemoveGadget(ColorWindow,
  287.                 &ColorTemplateGadgets[COLOR_RED]);
  288.         greenpos = RemoveGadget(ColorWindow,
  289.                 &ColorTemplateGadgets[COLOR_GREEN]);
  290.         bluepos = RemoveGadget(ColorWindow,
  291.                 &ColorTemplateGadgets[COLOR_BLUE]);
  292.         SetColorProps();
  293.         AddGadget(ColorWindow, &ColorTemplateGadgets[COLOR_BLUE], bluepos);
  294.         AddGadget(ColorWindow, &ColorTemplateGadgets[COLOR_GREEN], greenpos);
  295.         AddGadget(ColorWindow, &ColorTemplateGadgets[COLOR_RED], redpos);
  296.         RefreshGadgets(&ColorTemplateGadgets[COLOR_GADGETS_COUNT - 1], 
  297.                 ColorWindow);
  298.         }
  299.  
  300.     else switch (gadget->GadgetID)
  301.         {
  302.         case COLOR_OK:
  303.             CloseColorWindow(TRUE);
  304.             return(FALSE);
  305.             break;
  306.         case COLOR_CANCEL:
  307.             CloseColorWindow(FALSE);
  308.             return(FALSE);
  309.             break;
  310.         case COLOR_COPY:
  311.             ColorMode = COPYCOLOR;
  312.             break;
  313.         case COLOR_RANGE:
  314.             ColorMode = RANGE_FIRST;
  315.             break;
  316.         }
  317.     return(TRUE);
  318. }
  319.  
  320.  
  321. ModifyColors()
  322. {
  323.     USHORT pen, newred, newgreen, newblue;
  324.  
  325.     pen = w->RPort->FgPen;
  326.  
  327.     newred = ((struct PropInfo *)
  328.             ColorTemplateGadgets[COLOR_RED].SpecialInfo)->HorizPot >> 12;
  329.     newgreen = ((struct PropInfo *)
  330.             ColorTemplateGadgets[COLOR_GREEN].SpecialInfo)->HorizPot >> 12;
  331.     newblue = ((struct PropInfo *)
  332.             ColorTemplateGadgets[COLOR_BLUE].SpecialInfo)->HorizPot >> 12;
  333.  
  334.     SetRGB4(vp, pen, newred, newgreen, newblue);
  335. }
  336.  
  337.  
  338.  
  339. DrawColorWindow()
  340. {
  341.     struct RastPort *RPort;
  342.  
  343.     RPort = ColorWindow->RPort;
  344.  
  345.     ColorRectFill(RPort, rp->FgPen);
  346.     SetAPen(RPort, 1);
  347.     DrawBox(RPort, 1, 1, COLORWINDOW_WIDTH - 2, COLORWINDOW_HEIGHT - 2);
  348.     DrawBox(RPort, COLOR_BOX_LEFT - 2, COLOR_BOX_TOP - 2, 
  349.         COLOR_BOX_RIGHT + 2, COLOR_BOX_BOTTOM + 2);
  350.     DrawBox(RPort, COLOR_BOX_LEFT - 2, COLOR_COLOR_TOP - 2, 
  351.         COLOR_BOX_LEFT + (8 * 15) + 1, COLOR_COLOR_TOP + (4 * 10) + 1);
  352. }
  353.  
  354.  
  355.  
  356. SetColorProps()
  357. {
  358.     USHORT rgb, red, green, blue;
  359.  
  360.     rgb = GetRGB4(vp->ColorMap, rp->FgPen);
  361.     red = (rgb >> 8) & 0xF;
  362.     green = (rgb >> 4) & 0xF;
  363.     blue = (rgb >> 0) & 0xF;
  364.  
  365.     ((struct PropInfo *)ColorTemplateGadgets[COLOR_RED]
  366.             .SpecialInfo)->HorizPot
  367.             = (red << 12) | (red << 8) | (red << 4) | red;
  368.     ((struct PropInfo *)ColorTemplateGadgets[COLOR_GREEN]
  369.             .SpecialInfo)->HorizPot
  370.             = (green << 12) | (green << 8) | (green << 4) | green;
  371.     ((struct PropInfo *)ColorTemplateGadgets[COLOR_BLUE]
  372.             .SpecialInfo)->HorizPot
  373.             = (blue << 12) | (blue << 8) | (blue << 4) | blue;
  374. }
  375.  
  376.  
  377. ColorRectFill(RPort, pen)
  378. struct RastPort *RPort;
  379. SHORT pen;
  380. {
  381.     SetAPen(RPort, pen);
  382.     SetDrMd(RPort, JAM2);
  383.     WaitBOVP(vp);
  384.     RectFill(RPort, COLOR_BOX_LEFT, COLOR_BOX_TOP, 
  385.         COLOR_BOX_RIGHT, COLOR_BOX_BOTTOM);
  386. }
  387.  
  388.  
  389. VOID DoColorWindow()
  390. {
  391.     struct IntuiMessage *message;
  392.     ULONG class;
  393.     struct Gadget *gadget;
  394.  
  395.     if (NOT OpenColorWindow()) return;
  396.  
  397.     FOREVER
  398.         {
  399.         Wait((1 << ColorWindow->UserPort->mp_SigBit));
  400.         while (message = (struct IntuiMessage *)GetMsg(ColorWindow->UserPort))
  401.             {
  402.             class = message->Class;
  403.             gadget = (struct Gadget *)(message->IAddress);
  404.             ReplyMsg(message);
  405.  
  406.             switch (class)
  407.                 {
  408.                 case GADGETDOWN:
  409.                 case GADGETUP:
  410.                     if (ColorGadgetGotten(gadget) == FALSE) return;
  411.                     break;
  412.                 case MOUSEMOVE:
  413.                     ModifyColors();
  414.                     break;
  415.                 }
  416.             }
  417.         }
  418. }
  419.  
  420.  
  421. InitSuperColors()
  422. {
  423.     SHORT i;
  424.  
  425.     /* OK, initialize those Super Color Gadgets! */
  426.     for (i = 0; i < 32; i++)
  427.         {
  428.         SuperColorImages[i].LeftEdge = 0;
  429.         SuperColorImages[i].TopEdge = 0;
  430.         SuperColorImages[i].Width = 15;
  431.         SuperColorImages[i].Height = 10;
  432.         SuperColorImages[i].Depth = 0;
  433.         SuperColorImages[i].ImageData = NULL;
  434.         SuperColorImages[i].PlanePick = 0;
  435.         SuperColorImages[i].PlaneOnOff = i;
  436.         SuperColorImages[i].NextImage = NULL;
  437.         }
  438. }
  439.